<?php

namespace app\services\distributor;

use app\dao\distributor\DistributorDao;
use app\services\BaseServices;
use app\model\distributor\DistributorApplication as DistributorApplicationModel;
use crmeb\exceptions\AdminException;
use crmeb\exceptions\ApiException;

class DistributorApplicationService extends BaseServices
{

    public function __construct(DistributorDao $dao)
    {
        $this->dao = $dao;
    }

    public function getAuditList(array $where)
    {
        $nickname = $where['nickname'] ?? '';
        $add_time = $where['add_time'] ?? [];

        $applications = DistributorApplicationModel::hasWhere('user', function ($query) use ($nickname) {
                if ($nickname) {
                    $query->where('User.nickname', 'like', '%' . $nickname . '%');
                }
            })->when((!empty($add_time) && count($add_time) == 2), function ($query) use ($add_time) {
                // 明确指定是 DistributorApplicationModel 表的 add_time 字段
                $query->where('zg_distributor_application.add_time', '>=', $add_time[0])
                    ->where('zg_distributor_application.add_time', '<=', $add_time[1]);
            })->paginate($where['limit'] ?? 15);


        $list = [];
        foreach ($applications->items() as $item) {
            $item['nickname'] = ($item->user) ? $item->user->nickname : '';
            unset($item['user']);
            $list[] = $item;
        }

        return [
            'list'  =>  $list,
            'count' =>  $applications->count()
        ];
    }

    public function audit($ids, $handling_comments)
    {
        $distributorApplications = DistributorApplicationModel::whereIn('id', $ids)->select();

        foreach ($distributorApplications as $distributorApplication) {
            if (!$distributorApplication) throw new AdminException(30092);
            if ($distributorApplication->has_deal == 1) throw new AdminException(30093);
        }

        return DistributorApplicationModel::whereIn('id', $ids)->update([
            'handling_comments'    =>  $handling_comments,
            'has_deal'  =>  1,
            'audit_status'  =>  1
        ]);


    }


    public function apply($user, $application_reason, $contact_phone)
    {
        if (!$user) {
            throw new ApiException(410044);
        }
        if (!$application_reason) {
            throw new ApiException(31017);
        }
        if (!$contact_phone) {
            throw new ApiException(31018);
        }

        $distributor_application = DistributorApplicationModel::where('uid', $user->uid)->find();
        if ($distributor_application && $distributor_application->audit_status == 1) {
            throw new ApiException(31015);
        }
        if ($distributor_application && $distributor_application->audit_status == 0) {
            throw new ApiException(31016);
        }

        DistributorApplicationModel::create([
            'contact_phone' =>  $contact_phone,
            'application_reason'    =>  $application_reason,
            'audit_status'   => 0,
            'uid' => $user->uid,
            'add_time' => time()
        ]);

    }

}